package 富婆梦.第一阶段.第三周.作业;

/**
 * （1）这是一个描述ArrayList的类
 * （2）属性
 * 1.length
 * 2.Object[]
 * （3）方法
 * 1.MengArrayList()构造函数
 * 2.add(element)
 * 3.add(index,element)
 * 4.get(index)
 * 5.size()
 * 6.indexOf(element)
 * 7.remove(index)
 * 8.remove(element)
 * 9.isEmpty()
 */
public class MyArrayList {
    private int length;
    private Object[] object = new Object[length];

    //add
    public Object[] add(Object element) {
        length = length + 1;
        Object[] x = new Object[length];
        x[length - 1] = element;
        for (int i = 0; i < length - 1; i++) {
            x[i] = object[i];
        }
        object = x;
        return object;
    }

    public Object[] add(int index, Object element) {
        if (index > length) {
            System.out.println("数组越界！操作失败！！！");
        } else {
            length = length + 1;
            Object[] x = new Object[length];
            for (int i = 0; i < index; i++) {
                x[i] = object[i];
            }
            x[index] = element;
            for (int i = index; i < length - 1; i++) {
                x[i + 1] = object[i];
            }
            object = x;
        }
        return object;
    }

    //get
    public Object get(int index) {
        if (index > length - 1) {
            System.out.println("数组越界!");
            return null;
        } else {
            return object[index];
        }
    }

    public int size() {
        return object.length;
    }

    public int indexOf(Object element) {
        for (int i = 0; i < object.length; i++) {
            if (object[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public boolean isEmpty() {
        if (length == 0) {
            return true;
        }
        return true;
    }

    //remove
    public Object[] remove(int index) {
        if (index > length - 1 || index < 0) {
            System.out.println("数组越界！操作失败！！！");
        } else {
            length = length - 1;
            Object[] x = new Object[length];
            for (int i = 0; i < index; i++) {
                x[i] = object[i];
            }
            for (int i = index; i < length; i++) {
                x[i] = object[i + 1];
            }
            object = x;
        }
        return object;
    }

    public Object[] remove(Object element) {
        int index = 0;
        for (int i = 0; i < length; i++) {
            if (object[i] == element) {
                index = i;
            }
        }
        return remove(index);
    }

}
